package com.gjad.www.service;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.service.BaseService;
import com.eastdigit.util.StringUtil;
import com.gjzt.business.media.service.MediaResourceMarksService;

@Service
public class MediaQueryService extends BaseService {
	
	@Autowired
	private MediaResourceMarksService mediaResourceMarksService;
	
	/**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONObject findLineMedias(ReqBean reqBean) {
    	StringBuffer sql = new StringBuffer("select * from MEDIA_LINE t where 1 = 1 ");
        if(!StringUtil.isEmpty(reqBean.getInfo("mediaLevel"))){
        	sql.append(" and t.MEDIA_LEVEL = '").append(reqBean.getInfo("mediaLevel")).append("'");
        }
        if(!StringUtil.isEmpty(reqBean.getInfo("mediaMarks"))){
        	JSONArray markIdArray = JSONArray.fromObject(reqBean.getInfo("mediaMarks"));
        	String markIds = "";
        	for(Object o : markIdArray){
        		String markId = (String)o.toString();
        		markIds += (markIds == "" ? "" : ",") + ("'" + markId + "'");
        	}
        	if(!StringUtil.isEmpty(markIds)){
        		sql.append(" and exists ( select * from MEDIA_RESOURCE_MARKS m where t.LINE_ID = m.LINE_ID");
            	sql.append(" 		and m.MARK_ID in (" + markIds + ")");
            	sql.append("	)");
        	}
        }
        sql.append(" order by t.LINE_NAME");
        JSONObject result = this.queryTable(sql.toString(),null,reqBean.getInfoInt("pageSize"), reqBean.getInfoInt("pageIndex"));
        if(result != null){
        	JSONArray rows = result.getJSONArray("rows");
        	if(rows != null && !rows.isEmpty()){
        		for(Object line : rows){
        			JSONObject _line = (JSONObject)line;
        			String lineId = _line.getString("lineId");
        			_line.put("marks", mediaResourceMarksService.queryMarksOfLine(lineId));
        		}
        	}
        }
        return result;
        
    }
    
    /**
     * 
     * @param reqBean
     * @return
     */
    public JSONObject findStopMedias(ReqBean reqBean) {
    	StringBuffer sql = new StringBuffer("select * from MEDIA_STOP t where 1 = 1 ");
        if(!StringUtil.isEmpty(reqBean.getInfo("mediaLevel"))){
        	sql.append(" and t.MEDIA_LEVEL = '").append(reqBean.getInfo("mediaLevel")).append("'");
        }
        if(!StringUtil.isEmpty(reqBean.getInfo("mediaMarks"))){
        	JSONArray markIdArray = JSONArray.fromObject(reqBean.getInfo("mediaMarks"));
        	String markIds = "";
        	for(Object o : markIdArray){
        		String markId = (String)o.toString();
        		markIds += (markIds == "" ? "" : ",") + ("'" + markId + "'");
        	}
        	if(!StringUtil.isEmpty(markIds)){
        		sql.append(" and exists ( select * from MEDIA_RESOURCE_MARKS m where t.STOP_ID = m.LINE_ID");
            	sql.append(" 		and m.MARK_ID in (" + markIds + ")");
            	sql.append("	)");
        	}
        }
        
        sql.append(" order by t.STOP_NAME");
        JSONObject result = this.queryTable(sql.toString(),null,reqBean.getInfoInt("pageSize"), reqBean.getInfoInt("pageIndex"));
        if(result != null){
        	JSONArray rows = result.getJSONArray("rows");
        	if(rows != null && !rows.isEmpty()){
        		for(Object stop : rows){
        			JSONObject _stop = (JSONObject)stop;
        			String stopId = _stop.getString("stopId");
        			_stop.put("marks", mediaResourceMarksService.queryMarksOfStop(stopId));
        		}
        	}
        }
        return result;
    }
}
